package com.inplaceedit.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiChild;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.Focusable;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HasOneWidget;
import com.google.gwt.user.client.ui.Widget;
import com.core.gwt.ui.client.IsFocusable;

public class InPlaceEditPanel extends Composite {

	interface Binder extends UiBinder<Widget, InPlaceEditPanel> {
	}

	private static Binder uiBinder = GWT.create(Binder.class);

	@UiField
	DeckPanel container;
	@UiField
	CommandPanel displayPanel;
	@UiField
	Anchor editCommand;
	@UiField
	HTMLPanel deleteCommandPanel;
	@UiField
	Anchor deleteCommand;
	@UiField
	SaveCancelPanel editorPanel;

	SaveCancelDialog dialog;

	public InPlaceEditPanel() {
		initWidget(uiBinder.createAndBindUi(this));
		hideEditor();
	}

	public SaveCancelDialog asDialog() {
		if (dialog == null) {
			dialog = SaveCancelDialogAdapter.of(new Command() {
				@Override
				public void execute() {
					showEditor();
				}
			}, new Command() {
				@Override
				public void execute() {
					hideEditor();
				}
			}, getSaveCommand(), getCancelCommand());
		}
		return dialog;
	}

	public HasClickHandlers getCancelCommand() {
		return editorPanel.getCancelCommand();
	}

	public HasClickHandlers getDeleteCommand() {
		return deleteCommand;
	}

	public HasOneWidget getDisplayPanel() {
		return displayPanel.getContentPanel();
	}

	public HasClickHandlers getEditCommand() {
		return editCommand;
	}

	public HasOneWidget getEditorPanel() {
		return editorPanel.getContentPanel();
	}

	public HasClickHandlers getSaveCommand() {
		return editorPanel.getSaveCommand();
	}

	public void hideEditor() {
		container.showWidget(0);
	}

	public boolean isDeleteCommandVisible() {
		return deleteCommandPanel.isVisible();
	}

	public void setDeleteCommandVisible(final boolean visible) {
		deleteCommandPanel.setVisible(visible);
	}

	@UiChild(limit = 1, tagname = "display")
	public void setDisplayWidget(final Widget w) {
		displayPanel.getContentPanel().setWidget(w);
	}

	@UiChild(limit = 1, tagname = "editor")
	public void setEditorWidget(final Widget w) {
		editorPanel.getContentPanel().setWidget(w);
	}

	public void showEditor() {
		container.showWidget(1);
		final Widget w = editorPanel.getContentPanel().getWidget();
		if (w != null) {
			if (w instanceof Focusable) {
				((Focusable) w).setFocus(true);
			} else if (w instanceof IsFocusable) {
				final Focusable f = ((IsFocusable) w).asFocusable();
				if (f != null) {
					f.setFocus(true);
				}
			}
		}
	}
}
